/**
 * Created by zhangjinrui on 17/6/27.
 */

public class Solution236 {

    public static void main(String args[]) {
        Solution236 s = new Solution236();
    }

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null || root == p || root == q) {
            return root;
        }
        TreeNode l = lowestCommonAncestor(root.left, p, q);
        TreeNode r = lowestCommonAncestor(root.right, p, q);
        if(l != null && r != null){
            return root;
        }
        return l == null ? r : l;
    }

}
